#include "list_operate.h"


/*============================客户端链表操作============================*/
/*
    添加客户端结点：头插法
    参数：1.链表头结点
         2.新客户端结点的套接字描述符
*/
void add_list_client(Client_Node *head, int connfd)
{
    //申请一个新的结点内存空间
    Client_Node *p = (Client_Node *)malloc(sizeof(Client_Node));
    p->connfd = connfd;

    //头插法
    p->next = head->next;
    head->next = p;
}

/*
    删除客户端结点
    参数：1.链表头结点
         2.待删除的套接字描述符
*/
void del_list_client(Client_Node *head, int connfd)
{
    Client_Node *p = head;
    Client_Node *q = p->next;
    while(q != NULL)
    {
        if(q->connfd != connfd){
            p = q;
            q = p->next;
            continue;
        }else{
            p->next = q->next;
            free(q);
        }
    }
}

/*============================已注册用户链表操作============================*/
/*
    添加用户结点：头插法
*/
void add_list_user(User_Node *head, char *name, char *passwd)
{
    //申请一个新的结点内存空间
    User_Node *p = (User_Node *)malloc(sizeof(User_Node));
    memcpy(p->name, name, sizeof(p->name));
    memcpy(p->passwd, passwd, sizeof(p->passwd));

    //头插法
    p->next = head->next;
    head->next = p;
}

/*============================在线用户链表操作============================*/
/*
    添加用户结点：头插法
*/
void add_list_online_user(Online_User_Node *head, int connfd, char *name, char *passwd)
{
    //申请一个新的结点内存空间
    Online_User_Node *p = (Online_User_Node *)malloc(sizeof(Online_User_Node));
    memcpy(p->name, name, sizeof(p->name));
    memcpy(p->passwd, passwd, sizeof(p->passwd));
    p->connfd = connfd;

    //头插法
    p->next = head->next;
    head->next = p;
}

/*
    用户下线,删除离线用户结点
*/
void del_list_online_user(Online_User_Node *head, int connfd)
{
    Online_User_Node *p = head;
    Online_User_Node *q = p->next;
    while(q != NULL)
    {
        if(q->connfd != connfd){
            p = q;
            q = p->next;
            continue;
        }else{
            p->next = q->next;
            free(q);
        }
    }
}

